package com.hsu.example.provider;

import com.hsu.example.common.service.UserService;
import com.hsu.hsurpc.RpcApplication;
import com.hsu.hsurpc.config.RegistryConfig;
import com.hsu.hsurpc.config.RpcConfig;
import com.hsu.hsurpc.model.ServiceMetaInfo;
import com.hsu.hsurpc.registry.LocalRegistry;
import com.hsu.hsurpc.registry.Registry;
import com.hsu.hsurpc.registry.RegistryFactory;
import com.hsu.hsurpc.server.HttpServer;
import com.hsu.hsurpc.server.VertxHttpServer;
import com.hsu.hsurpc.server.tcp.VertxTcpServer;

/**
 * 简易服务提供者示例
 * @Author Hsu琛君珩
 * @Date 2024-09-18 14:13
 * @Description
 * @Version: v1.0.0
 */
public class ProviderExample {

    public static void main(String[] args) {
        // 初始化 RPC 框架
        RpcApplication.init();

        // 本地注册服务
        String serviceName = UserService.class.getName();
        LocalRegistry.register(serviceName, UserServiceImpl.class);

        // 注册服务到注册中心
        RpcConfig rpcConfig = RpcApplication.getRpcConfig();
        RegistryConfig registryConfig = rpcConfig.getRegistryConfig();
        Registry registry = RegistryFactory.getInstance(registryConfig.getRegistry());

        ServiceMetaInfo serviceMetaInfo = new ServiceMetaInfo();
        serviceMetaInfo.setServiceName(serviceName);
        serviceMetaInfo.setServiceHost(rpcConfig.getServerHost());
        serviceMetaInfo.setServicePort(rpcConfig.getServerPort());

        try {
            registry.register(serviceMetaInfo);  // 服务注册
            System.out.println("服务注册成功：" + serviceMetaInfo.getServiceAddress());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        // 启动 TCP 服务
        VertxTcpServer vertxTcpServer = new VertxTcpServer();
        vertxTcpServer.doStart(rpcConfig.getServerPort());
        System.out.println("TCP 服务器启动成功，监听端口: " + rpcConfig.getServerHost());
    }
}
